package com.kyrie.lib_sys.utils.learn.TimeDemo;

import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class TimeDemo08 {

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 时间区间重叠（按员工分组）
     *
     * @param timeList
     * @return boolean
     */
    public static boolean timeListCheck(List<Map<String, String>> timeList) {

        //定义一个map :  key 是员工 ID，value 是每位员工对应的时间区间列表 (1 ,  prepareTime , 2025:07:01 09:00:00)
        Map<String, List<Map<String, Object>>> employeeTimeMap = new HashMap<>();

        //遍历timeList集合
        for (int i = 0; i < timeList.size(); i++) {
            Map<String, String> task = timeList.get(i);
            String pk_psndoc = task.getOrDefault("pk_psndoc", "unknown");

            //定义一个timePoints集合: 存储所有时间点
            List<LocalDateTime> timePoints = new ArrayList<>();

            String prepareTime = task.get("prepareTime");
            if (prepareTime != null && !"".equals(prepareTime)) {
                timePoints.add(LocalDateTime.parse(prepareTime, formatter));
            }
            String travelTime = task.get("travelTime");
            if (travelTime != null && !"".equals(travelTime)) {
                timePoints.add(LocalDateTime.parse(travelTime, formatter));
            }
            String startTime = task.get("startTime");
            if (startTime != null && !"".equals(startTime)) {
                timePoints.add(LocalDateTime.parse(startTime, formatter));
            }
            String completeTime = task.get("completeTime");
            if (completeTime != null && !"".equals(completeTime)) {
                timePoints.add(LocalDateTime.parse(completeTime, formatter));
            }
            

            //timePoints不为空,走下面逻辑
            if (!timePoints.isEmpty()) {
                LocalDateTime start = Collections.min(timePoints);
                LocalDateTime end = Collections.max(timePoints);

                //定义一个Map，用于存储当前时间区间
                Map<String, Object> currentIntervalMap = new HashMap<>();
                currentIntervalMap.put("start", start);
                currentIntervalMap.put("end", end);

                //添加记录序号(用于记录当前插入数据是哪一条记录)
                currentIntervalMap.put("index", i + 1);

                //定义一个existingMap 存储当前员工已有的所有时间区间（如果不存在则返回空列表）
                List<Map<String, Object>> existingMap = employeeTimeMap.get(pk_psndoc);
                if (StringUtils.isEmpty(existingMap)) {
                    existingMap = new ArrayList<>();
                }

                //遍历existingMap,获取已存在的每一个时间区间
                for (Map<String, Object> exist : existingMap) {
                    LocalDateTime existStart = (LocalDateTime) exist.get("start");
                    LocalDateTime existEnd = (LocalDateTime) exist.get("end");
                    int existIndex = (int) exist.get("index");

                    //当前时间区间是否与某个已存在的时间区间重叠
                    if (!(end.isBefore(existStart) || start.isAfter(existEnd))) {
                        System.out.printf("员工 [%s] 的记录%d[%s ~ %s] 与记录%d[%s ~ %s] 存在时间重叠错误\n",
                                pk_psndoc,
                                (int) currentIntervalMap.get("index"), formatter.format(start), formatter.format(end),
                                existIndex, formatter.format(existStart), formatter.format(existEnd)
                        );
                        return false;
                    }
                }

                existingMap.add(currentIntervalMap);
                employeeTimeMap.put(pk_psndoc, existingMap);
            }
        }

        return true;
    }


    public static void main(String[] args) {
        List<Map<String, String>> timeList = new ArrayList<>();

        Map<String, String> time1 = new HashMap<>();
        time1.put("pk_psndoc", "E01");
        time1.put("prepareTime", "2025-07-01 09:00:00");
        time1.put("travelTime", "2025-07-01 09:10:00");
        time1.put("completeTime", "2025-07-01 09:30:00");

        Map<String, String> time2 = new HashMap<>();
        time2.put("pk_psndoc", "E01");
        time2.put("prepareTime", "2025-07-01 09:20:00"); // 与 time1 有重叠

        Map<String, String> time3 = new HashMap<>();
        time3.put("pk_psndoc", "E02");
        time3.put("prepareTime", "2025-07-01 10:00:00");
        time3.put("startTime", "2025-07-01 10:30:00");

        timeList.add(time1);
        timeList.add(time2);
        timeList.add(time3);

        boolean result = timeListCheck(timeList);
        System.out.println("校验结果: " + result);
    }

}
